package top.opm.controller;

import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import top.jx.dao.FileInfoDao;
import top.jx.model.FileInfo;
import top.opm.dao.WindowsServerDao;
import top.opm.entity.WindowsServer;
import top.opm.model.ResultData;
import top.opm.service.WindowsServerService;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.UUID;

@RestController
@RequestMapping(value = "/deployWindows")
@Api(tags = "部署信息")
public class DeloyWindowController {
    @Autowired
    private WindowsServerDao windowsServerDao;

    @Autowired
    private WindowsServerService windowsServerService;


    @Autowired
    private FileInfoDao fileInfoDao;

    @GetMapping(value = "/getDeployWindowsList")
    @ApiOperation(value = "服务器列表", notes = "部署列表")
    public ResultData getDeployWindowsList() {
        ArrayList<Object> objects = new ArrayList<>();
        List<WindowsServer> all = windowsServerDao.findAll(Sort.by(Sort.Direction.ASC, "ip", "tomcatName", "tomcatPath"));
        return ResultData.success(all);
    }

    @PostMapping(value = "/saveDeployWindows")
    @ApiOperation(value = "保存服务器信息", notes = "部署列表")
//    @ApiImplicitParams({
//            @ApiImplicitParam(dataType = "datas", name = "datas", value = "datas"),
//    })
    public ResultData saveDeployWindows(@RequestBody List<WindowsServer> datas) {
        System.out.println(datas);
        for (WindowsServer win : datas) {
            if (Objects.isNull(win.getId())) {
            }
        }
        windowsServerDao.saveAll(datas);
        return ResultData.success();
    }


    @GetMapping(value = "/getFiles")
    @ApiOperation(value = "获取hotfix", notes = "获取hotfix")
    public ResultData getFiles() {
        List<FileInfo> all = fileInfoDao.findAll(Sort.by(Sort.Direction.ASC, "name"));
        return ResultData.success(all);
    }


    @PostMapping(value = "/doStart")
    @ApiOperation(value = "启动tomcat", notes = "获取hotfix")
    @ApiImplicitParam(paramType = "query", name = "file", value = "hotfix", required = true, dataType = "file", defaultValue = "file")
    public ResultData doStart(@RequestBody List<WindowsServer> windowsServerData) {
        windowsServerService.doStart(windowsServerData);
        return ResultData.success();
    }

    @PostMapping(value = "/doStop")
    public ResultData doStop(@RequestBody List<WindowsServer> datas) {
        windowsServerService.doStop(datas);
        return ResultData.success();
    }

    @PostMapping(value = "/executeKill")
    public ResultData executeKill(@RequestBody List<WindowsServer> datas) {
        windowsServerService.executeKill(datas.get(0));
        return ResultData.success();
    }

    @PostMapping(value = "/queryStatus")
    public ResultData queryStatus(@RequestBody List<WindowsServer> datas) {
        for (WindowsServer window : datas) {
            windowsServerService.queryStatus(window);
        }
        return ResultData.success();
    }
}
